<?php
class system_controller
{
	public $_ = array();
    public $_layout = NULL;
    public $part_root=1;
    public $_act = NULL;
	public $_seokey = array();

    public $_content_top = NULL;
    public $_content_bot = NULL;
    public $_content_right = NULL;
    public $_content_left = NULL;
    public $_content_header = NULL;
    public $_content_footer = NULL;


	public function __construct()
    {		
		$this->request = new handle_request();
		$this->view = new system_view();
				       
        if(file_exists(PATH_APP.APP_CURRENT.'/view/'.DIR_LANG.'/'.DIR_LANG_PUBLIC_DEFAULT.'/system.php')) 
        {
        	require_once(PATH_APP.APP_CURRENT.'/view/'.DIR_LANG.'/'.DIR_LANG_PUBLIC_DEFAULT.'/system.php'); 
        } 
        else {
        	echo 'no lang system.php';
        }
		
        $this->_ = $_;
	}
    
    public function index_action()
    {     
        /*
            Neu co seokey la rong.
            The vi tri cua component la $_i.
            $_j lay vi tri seo cua js va css.
			$_k lay vi tri cua act;
        */
        if(SEO_CURRENT==''|| SEO_CURRENT==NULL)
		{
		/*App DF*/
			$_i = 0;
			
            /*Get act in seokey*/
            $_skact = isset($this->_seokey[$_i+1])?$this->_seokey[$_i+1]:NULL;
            
            /*Mang router path mac dinh*/
            $SEO_CAT= isset($this->_seokey[$_i])?$this->_seokey[$_i]:NULL;
            $arr_path = $this->model->model_get_router($SEO_CAT);
            if(empty($arr_path)){
				/*Neu ko phai com thi lay com index boi seoky bang '' */
                $SEO_CAT = '';
				$arr_path = $this->model->model_get_router($SEO_CAT);
                /*Get act in seokey*/
                $_skact = $this->_seokey[$_i];                
			}
            
			$_j= (APP_CURRENT==DIR_DEFAULT)?$_i:($_i+1);
            
			
			
		} else {  
		/*App Other*/	
		
			$_i = 1;
            $position_com = isset($this->_seokey[$_i])?$this->_seokey[$_i]:NULL;
            if( empty($position_com) )
            {
                $_i = $_i - 1;        
            }
            /*Get act in seokey*/
			$_skact = isset($this->_seokey[$_i+1])?$this->_seokey[$_i+1]:NULL;
            
			/*Mang router path mac dinh*/
            $SEO_CAT = $this->_seokey[$_i];
            $arr_path = $this->model->model_get_router($SEO_CAT);
			if(empty($arr_path)){
				/*Neu ko phai com thi lay com index boi seokey vi tri app */
				$_i = $_i-1;
				$SEO_CAT = $this->_seokey[$_i];
                $arr_path = $this->model->model_get_router($SEO_CAT);
                /*Get act in seokey*/
				$_skact = $this->_seokey[$_i+1];
                          
			}			
            $_j= $_i;
            
		}
                              
        /*Xay dung cau truc app, com, act*/
		$_app = isset($arr_path['AppName'])?$arr_path['AppName']:'';
		$_com = isset($arr_path['ComName'])?$arr_path['ComName']:'';
        $_app_id = $this->model->model_get_app_id($_app);
        $_com_id = $this->model->model_get_com_id($_com);

        /*Lay act tu seokey cua bang com*/
        $_actname_tbl_com = '';
        $last_seo = end($this->_seokey);
        $last_seo_notype = str_replace('.html','',$last_seo);
        $arr_tbl = $this->model->module_tbl_list();
		
        if( in_array(prefix.$_com,$arr_tbl) && $last_seo_notype!=$last_seo )
        {
            $_act_tbl_com = $this->model->model_get_tbl_act($_com,$_com_id,$last_seo_notype); 
            $_actname_tbl_com = $_act_tbl_com['ActName'];
        }

        /* Ket thuc Lay act tu bang cua com*/
        
        /*Xu ly act*/
        $_act = $this->model->model_get_action($_skact,$_com_id);
        if($_act){
            
            $_act = $_act.'_action';
        
        }else{ 
        
            if($_actname_tbl_com){
                
                   $_act = $_actname_tbl_com.'_action';
            }else{
                /*Lay act tu Seokey cua act*/
                $_act = $this->model->model_get_action($last_seo,$_com_id);
        		if( empty($_act) )
                {
                    $_act = isset($arr_path['ActName'])?$arr_path['ActName']."_action":'';
                    
                }else{
                    /*Mac dinh tu bang category*/
                    $_act = $_act.'_action';
                }
                
            }
        }
		
		/*seo com dung phan quyen*/
		define('COMCUR',$_com,true);
        define('AppID',$_app_id,true);
		define('ComID',$_com_id,true);
		define('SEO_CAT',$SEO_CAT,true);
		define('ACT',str_replace('_action','',$_act),true);
		
        $content_mid='';

        if($_com!='')
        {
            if( file_exists(PATH_LANGUAGE.'/'.$_com.'.php') )
            {
                require_once(PATH_LANGUAGE.'/'.$_com.'.php');
                $this->_ = empty($_)?$this->_:array_merge($this->_,$_);
                
            }else{
                
                echo 'No find file lang <strong>'.$_com.'.php</strong> of '.$_com; exit;
            }
            			
			$ojbctr = trim("controller_".$_com);
			$this->_ctr = new $ojbctr();
			
			/*Truyen cac lop qua com*/
			$this->_ctr->view = $this->view;
			$this->_ctr->route = $this->route;
			$this->_ctr->request = $this->request; 
			$this->_ctr->model = $this->model;
			$this->_ctr->_seokey = $this->_seokey;
			$this->_ctr->_ = !isset($this->_) && empty($this->_)?NULL:$this->_;

			/*Khoi dong com*/
			$content_mid = $this->_ctr->$_act();
            
            $this->_ = !isset($this->_ctr->_)&& empty($this->_ctr->_)?$this->_:array_merge($this->_,$this->_ctr->_);
            $this->_layuot = $this->_ctr->_layout;
            /*Bat position*/
            $this->_content_top =       isset($this->_ctr->_content_top) && !empty($this->_ctr->_content_top)?$this->_ctr->_content_top:NULL;
            $this->_content_bot =       isset($this->_ctr->_content_bot) && !empty($this->_ctr->_content_bot)?$this->_ctr->_content_bot:NULL;
            $this->_content_right =     isset($this->_ctr->_content_right) && !empty($this->_ctr->_content_right)?$this->_ctr->_content_right:NULL;
            $this->_content_left =      isset($this->_ctr->_content_left) && !empty($this->_ctr->_content_left)?$this->_ctr->_content_left:NULL;
            $this->_content_header =    isset($this->_ctr->_content_header) && !empty($this->_ctr->_content_header)?$this->_ctr->_content_header:NULL;
            $this->_content_footer =    isset($this->_ctr->_content_footer) && !empty($this->_ctr->_content_footer)?$this->_ctr->_content_footer:NULL;

            $this->_css = !isset($this->_ctr->_css)&& empty($this->_ctr->_css)?NULL:implode('|',$this->_ctr->_css);
            $this->_js = !isset($this->_ctr->_js)&& empty($this->_ctr->_js)?NULL:implode('|',$this->_ctr->_js);
                       
		}
        else {
            
		  echo 'No find Com <strong>'.$_com.'</strong>'; exit;
		}
        
        

		/*####### Xay dung chuoi HTML chinh theo tung loai layout ##########################*/
		$str_html=NULL;
        $_mod = '';
        $__layuot = empty($this->_layuot)?'index':$this->_layuot;
        /*&&&& Run No layout &&&&&&&&&&&&&&&&&&*/
        if($__layuot=='NO_LAYOUT')
        {
			$str_html = $str_html = $this->view->view_assign($content_mid,$this->_);
        }
        
        else {
        /*&&&& layout &&&&&&&&&&&&&&&&&&*/
			
			/*==== Run com ==============*/
			$this->_['content_top']     =   $this->_content_top != 'OPEN' ?NULL:$this->get_module($_app_id,$_com_id,'content_top');
			$this->_['content_bot']     =   $this->_content_bot != 'OPEN' ?NULL:$this->get_module($_app_id,$_com_id,'content_bot');
			$this->_['content_right']   =   $this->_content_right != 'OPEN' ?NULL:$this->get_module($_app_id,$_com_id,'content_right');
			$this->_['content_footer']  =   $this->_content_footer != 'OPEN' ?NULL:$this->get_module($_app_id,$_com_id,'content_footer');
			$this->_['content_header']  =   $this->_content_header != 'OPEN' ?NULL:$this->get_module($_app_id,$_com_id,'content_header');
			$this->_['content_left']    =   $this->_content_left != 'OPEN' ?NULL:$this->get_module($_app_id,$_com_id,'content_left');
            $this->_['content_mid']     =   $content_mid;
        
            if(!empty($this->arr_mod))
            foreach($this->arr_mod as $v)
            {
                $_arrmod["$v"] = $v;
            }
            if(!empty($_arrmod))
            foreach($_arrmod as $m)
            {
                $_mod .='|'.$m;
            }
            
            /*==== set js css ===========*/
            $this->_['js'] = $this->request->rq_set_js(URL_PREFIX.'lib/scripts.php?include='.urlencode(URL_BASE.'|'.URL_PREFIX.'--'.$_app.'|'.$_com.'--'.$_mod.'--'.$this->_js.'--'.$this->route->rt_get_route() ));
            $this->_['css'] = $this->request->rq_set_css(URL_PREFIX.'lib/styles.php?include='.urlencode(URL_BASE.'|'.URL_PREFIX.'--'.$_app.'|'.$_com.'--'.$_mod.'--'.$this->_css));
		    /*==== end set js css ===========*/
            
            /*Relase All layout*/    
			$str_html = $this->view->view_assign(
                /*Relase content_mid*/
                $this->view->view_assign(
                    /*read layuot*/
                    $this->view->view_display_layout($__layuot),$this->_
                ),$this->_
            );
            
        }/*####### END Xay dung chuoi HTML chinh theo tung loai layout ##########################*/
       
       
              
        return $str_html;
	}
    
    private function get_module($_app_id,$_com_id,$position)
	{
		$get_mod ='';
		$arr = $this->model->get_module($_app_id,$_com_id,$position);
        
		if( !empty($arr) )
		{
			foreach($arr as $mod)
			{
				$mod = $mod['Name'];
				$this->arr_mod[] = $mod;
                if($mod)
				{
				    if( file_exists(PATH_LANGUAGE.'/module/'.$mod.'.php') )
                    {
                        @require_once(PATH_LANGUAGE.'/module/'.$mod.'.php');
                        $this->_ = !isset($_) && empty($_)?$this->_:array_merge($this->_,$_);
                        
                    }else{
                        
                        echo 'No find file lang <strong>'.$mod.'.php</strong>'; exit;
                    }
					$controller = trim("controller_module_".$mod);
					$_mod = new $controller();
					
					/*Truyen bien qua mod*/
					$_mod->view = $this->view;
					$_mod->route = $this->route;
					$_mod->request = $this->request;
					$_mod->model = $this->model;
					$_mod->_ = $this->_;					
					$_mod->_seokey = $this->_seokey;
					
					$_mod->_miss_comp = empty($this->_ctr->_miss_comp)?'':$this->_ctr->_miss_comp;
					
					/*Khoi dong mod*/
					$get_mod .= $_mod->index();
                    $this->_ = isset($_mod->_) && !empty($_mod->_)?array_merge($this->_,$_mod->_):$this->_;
                    
				}
			}
			
			return $get_mod;
		}
	}
    
    
}